<?php
declare (strict_types=1);

namespace app\store\controller;

use think\response\Json;
use app\store\model\Supplier as SupplierModel;
use app\common\exception\BaseException;

/**
 * 商品管理控制器
 * Class Goods
 * @package app\store\controller
 */
class Supplier extends Controller
{
    /**
     * 供应商列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        // 获取列表记录
        $model = new SupplierModel;
        $list  = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 添加商品选择供应商列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function select(): Json
    {
        // 获取列表记录
        $model = new SupplierModel;
        $list  = $model->getListByCategory($this->request->param('categoryId'));
        return $this->renderSuccess($list->toArray());
    }

    /**
     * 供应商详情
     * @param int $supplierId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function detail(int $supplierId): Json
    {
        // 获取商品详情
        $model        = new SupplierModel;
        $supplierInfo = $model->getDetail($supplierId);
        return $this->renderSuccess(compact('supplierInfo'));
    }

    /**
     * 添加供应商
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function add(): Json
    {
        $model = new SupplierModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑供应商
     * @param int $supplierId
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function edit(int $supplierId): Json
    {
        // 供应商详情
        $model = SupplierModel::detail($supplierId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改供应商审核(1通过 0待审核 2拒绝)
     * @param array $supplierId 供应商id
     * @param  $status 1正常 2拒绝
     * @return Json
     */
    public function check(): Json
    {
        $data = $this->request->param();
        if(empty($data['supplierIds'])){
            return $this->renderError('供应商id不能为空');
        }
        $model = new SupplierModel;
        if (empty($data['status']) || !in_array($data['status'], [1, 2])) {
            return $this->renderError('审核状态不正确');
        }
        if($data['status']==2 && empty($data['remarks'])){
            return $this->renderError('备注不能为空');
        }

        if (!$model->setStatus($data)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        if ($data['status'] == 1) {
            return $this->renderSuccess('供应商审核成功,供应商已通过');
        } else if ($data['status'] == 2) {
            return $this->renderSuccess('审核成功,供应商未通过');
        }
    }

    /**
     * 删除商品
     * @param array $supplierIds
     * @return Json
     */
    public function delete(array $supplierIds): Json
    {
        $model = new SupplierModel;
        $data['status'] = 3;
        $data['supplierIds'] = $supplierIds;
        if (!$model->setStatus($data)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 供应商模糊查询
     * @param int $supplierName
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function search(): Json
    {
        // 获取列表记录
        $model = new SupplierModel;
        $list  = $model->getListBySupplierName($this->request->param('supplierName'));
        return $this->renderSuccess($list->toArray());
    }
}
